package com.share.example.selenium.schedule;


import com.alibaba.fastjson.JSON;
import com.share.common.utils.DateExtUtil;
import com.share.common.utils.DateUtil;
import com.share.example.selenium.model.amazon.seller.ClosePromoteForm;
import com.share.example.selenium.model.amazon.seller.ClosePromoteTask;
import com.share.example.selenium.model.amazon.seller.PromoteForm;
import com.share.example.selenium.service.IBaseConfService;
import com.share.example.selenium.service.amazon.seller.AmazonSellerPromotionService;
import com.share.example.selenium.utils.UsDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component


public class amazon_seller_schedule {
    @Autowired
    private IBaseConfService baseConfService;

    @Autowired
    private AmazonSellerPromotionService sellerPromotionService;

    //每天执行一次---删除历史任务
    @Scheduled(cron = "0 0 10 * * ?")
    public void batchClosePromotion() {
        String key = baseConfService.getUserCode() + "_closePromotion";
        if (!"enable".equals(baseConfService.getByCode(key))) {
            log.info("key={},没有配置", key);
            return;
        }
        Date date = DateExtUtil.addDay(-2);
        String usDateTime = UsDateUtil.toYYYYMMDDHHMMSS(date);

        ClosePromoteTask task = new ClosePromoteTask();
        task.setStartPage(4);
        task.setDealSize(3);
        task.setStartFilter2(usDateTime);
        List<ClosePromoteForm> list = sellerPromotionService.batchClosePromotion(false, task);
        log.info("关闭的任务信息:", JSON.toJSONString(list));
    }

    //每天执行一次---创建任务
    @Scheduled(cron = "0 0 20 * * ?")
    public void batchCreatePromotion() {
        String key = baseConfService.getUserCode() + "_createPromotion";
        if (!"enable".equals(baseConfService.getByCode(key))) {
            log.info("key={},没有配置", key);
            return;
        }

        String asin = baseConfService.getAsin();
        if (StringUtils.isBlank(asin)) {
            log.info("asin没有配置");
            return;
        }
        String dateTimeBegin = DateUtil.getDateTimeBegin(1);
        Date beginUsDateTime = UsDateUtil.strDT2Date(dateTimeBegin);
        Date endUsDateTime = DateUtil.getDayEnd(beginUsDateTime);

        List<PromoteForm> list = sellerPromotionService.batchCreatePromotion(asin, 55, beginUsDateTime, endUsDateTime, 8, 2);
        log.info("创建的任务列表:", JSON.toJSONString(list));
    }

    public static void main(String[] args) {
        String dateTimeBegin = DateUtil.getDateTimeBegin(1);
        Date usDateTime = UsDateUtil.strDT2Date(dateTimeBegin);
        String s = UsDateUtil.toYYYY_MM_DD_HH_MM_SS(usDateTime);

        Date date = DateExtUtil.addDay(-2);
        String usDateTime2 = UsDateUtil.toYYYYMMDDHHMMSS(date);
        System.out.println(dateTimeBegin);
        System.out.println(s);
        System.out.println(usDateTime2);
    }
}
